package io.renren.modules.business.controller;

import java.util.Map;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.business.entity.ESalaryEntity;
import io.renren.modules.business.service.ESalaryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 薪资表
 */
@RestController
@RequestMapping("/business/esalary")
public class ESalaryController extends AbstractController {
    @Autowired
    private ESalaryService eSalaryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eSalaryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 薪资列表查询
     * @param pageNum
     * @param pageSize
     * @param monthId
     * @param userId
     * @return
     */
    @GetMapping("/query")
    public R querySalaryDetail(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "monthId", required = false) String monthId,
                               @RequestParam(value = "userId", required = false) Long userId){
        // 非管理员只能看到自己的薪资
        if (getUserAuthority() != 1) {
            return R.ok().put("page", eSalaryService.querySalaryDetail(pageNum, pageSize, getUserId(), monthId));
        }
        // 管理员可以查看所有人的薪资
        return R.ok().put("page", eSalaryService.querySalaryDetail(pageNum, pageSize, userId, monthId));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		ESalaryEntity eSalary = eSalaryService.getById(id);

        return R.ok().put("eSalary", eSalary);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ESalaryEntity eSalary){
		eSalaryService.save(eSalary);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ESalaryEntity eSalary){
		eSalaryService.updateById(eSalary);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam(value = "id") Long id){
		eSalaryService.removeById(id);

        return R.ok();
    }

}
